// PalFrame.cpp : implementation file
//

#include "stdafx.h"
#include "U7FileTool.h"
#include "PalFrame.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CPalInfoDlgBar

IMPLEMENT_DYNAMIC(CPalInfoDlgBar, CDialogBar)

CPalInfoDlgBar::CPalInfoDlgBar()
{

}

CPalInfoDlgBar::~CPalInfoDlgBar()
{
}


BEGIN_MESSAGE_MAP(CPalInfoDlgBar, CDialogBar)
	ON_WM_HSCROLL()
END_MESSAGE_MAP()

void CPalInfoDlgBar::UpdatePalInfo( int nCurrent, int nTotal )
{
	CWnd* pInfoWnd = GetDlgItem(IDC_STATIC_PALINFO);
	if ( pInfoWnd )
	{
		CString s;
		s.Format( _T("Current: %d, Total: %d"), nCurrent+1, nTotal );
		pInfoWnd->SetWindowText(s);
	}

	CSliderCtrl* pSliderWnd = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_PAL);
	if ( pSliderWnd )
	{
		pSliderWnd->SetRange( 1, nTotal );
		pSliderWnd->SetPos( nCurrent + 1 );
	}
}

int CPalInfoDlgBar::GetCurrentPalIndex()
{
	CSliderCtrl* pSliderWnd = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_PAL);
	if ( pSliderWnd )
	{
		int pos = pSliderWnd->GetPos();
		int min = pSliderWnd->GetRangeMin();
		return pos - min;
	}
	return 0;
}

// CPalInfoDlgBar message handlers
void CPalInfoDlgBar::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	if ( pScrollBar == GetDlgItem(IDC_SLIDER_PAL) )
	{
		CSliderCtrl* pSlider = (CSliderCtrl*)pScrollBar;
		int pos = pSlider->GetPos();
		int min, max;
		pSlider->GetRange( min, max );
		int total = max - min + 1;
		UpdatePalInfo(pos-min,total);

		CWnd* pParent = GetParent();
		while ( !pParent->IsKindOf(RUNTIME_CLASS(CPalFrame)) )
			pParent = pParent->GetParent();
		CDocument* pDoc = ((CPalFrame*)pParent)->GetActiveDocument();
		pDoc->UpdateAllViews(NULL);
	}
	else
	{
		CDialogBar::OnHScroll(nSBCode, nPos, pScrollBar);
	}
}





// CPalFrame

IMPLEMENT_DYNCREATE(CPalFrame, CMDIChildWnd)

CPalFrame::CPalFrame()
{

}

CPalFrame::~CPalFrame()
{
}


BEGIN_MESSAGE_MAP(CPalFrame, CMDIChildWnd)
	ON_WM_CREATE()
END_MESSAGE_MAP()



// CPalFrame message handlers


BOOL CPalFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying the CREATESTRUCT cs
	if( !CMDIChildWnd::PreCreateWindow(cs) )
		return FALSE;

	return TRUE;
}

int CPalFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	if( !m_wndToolBar.Create(
		this,
		IDD_DB_PALVIEW,
		CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY,
		IDD_DB_PALVIEW) )
	{
		TRACE0("Failed to create the toolbox\n");
		return -1;
	}

	m_wndToolBar.SetWindowText(_T("Toolbox"));
	m_wndToolBar.EnableDocking(CBRS_ALIGN_TOP|CBRS_ALIGN_BOTTOM);

	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);


	return 0;
}

void CPalFrame::UpdatePalInfo( int nCurrent, int nTotal )
{
	m_wndToolBar.UpdatePalInfo(nCurrent,nTotal);
}

int CPalFrame::GetCurrentPalIndex()
{
	return m_wndToolBar.GetCurrentPalIndex();
}






